﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Data;

/// <summary>
/// 字段对象工厂
/// </summary>
public static class FieldFactory
{
    private const int DescIndex = 0;
    private const int TypeIndex = 1;
    private const int ASCNIndex = 2;
    private const int NameIndex = 3;

    /// <summary>
    /// 获取字段对象List
    /// </summary>
    public static List<FieldBase> GetFieldList(string className, DataTable sheet)
    {
        List<FieldBase> fbList = new List<FieldBase>();
        Debug.Log("写入新建对象方法的输入参数区");

        int columns = sheet.Columns.Count;

        for (int i = 0; i < columns; i++)
        {
            string desc = sheet.Rows[DescIndex][i].ToString();
            string type = sheet.Rows[TypeIndex][i].ToString();
            string ASCN = sheet.Rows[ASCNIndex][i].ToString();
            string name = sheet.Rows[NameIndex][i].ToString();

            if (ASCN.ToUpper() == "A" || ASCN.ToUpper() == "C")
            {
                FieldBase fb = GetField(className, desc, type, name);
                fbList.Add(fb);
            }
        }

        return fbList;
    }

    public static FieldBase GetField(string className, string desc, string type, string name)
    {
        FieldBase fb = null;
        string lowerType = type.ToLower();

        if (lowerType.StartsWith("int"))
        {
            fb = new IntField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("float"))
        {
            fb = new FloatField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("stringarea"))
        {
            fb = new StringAreaField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("string"))
        {
            fb = new StringField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("enum"))
        {
            fb = new EnumField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("bool"))
        {
            fb = new BoolField(className, desc, type, name);
        }
        else if (lowerType.StartsWith("vector2"))
        {
            fb = new Vector2Field(className, desc, type, name);
        }
        else if (lowerType.StartsWith("vector3"))
        {
            fb = new Vector3Field(className, desc, type, name);
        }
        else if (lowerType.StartsWith("propset"))
        {
            fb = new PropSetField(className, desc, type, name);
        }
        else
        {
            Debug.LogError(string.Format("[Error]FieldFactory::GetField. type is not exist! className = {0} -- desc = {1} -- type = {2} -- name = {3}", className, desc, type, name));
        }

        return fb;
    }
}
